﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SDMX_Structure = SDMX_ML.Framework.Structure;
using SDMX_Common = SDMX_ML.Framework.Common;

namespace SDMX_ML.Framework.Messages
{
    internal class ElementBuilder
    {
  
        internal static XElement GetTextType(SDMX_Common.TextType textType, string name, XNamespace ns)
        {
            XElement enElement = null;

            if (textType.Text != null)
            {
                enElement = new XElement(ns + name, textType.Text);

                if (textType.Lang != null)
                {
                    enElement.Add(new XAttribute(XNamespace.Xml + "lang", textType.Lang));
                }
            }

            return enElement;
        }

        internal static XElement GetAnnotations(SDMX_Common.AnnotationsType annotations, XNamespace ns)
        {
            XElement enAnnotations = new XElement(ns + "Annotations");

            foreach (SDMX_Common.AnnotationType annotationType in annotations.Annotation)
            {

                XElement annotation = new XElement(Namespaces.GetNS("common") + "Annotation");

                if (annotationType.AnnotationTitle != null)
                {
                    XElement annTitle = new XElement(Namespaces.GetNS("common") + "AnnotationTitle");
                    annTitle.Value = annotationType.AnnotationTitle;
                    annotation.Add(annTitle);
                }

                if (annotationType.Annotationtype != null)
                {
                    XElement annType = new XElement(Namespaces.GetNS("common") + "AnnotationType");
                    annType.Value = annotationType.Annotationtype;
                    annotation.Add(annType);
                }

                if (annotationType.AnnotationUrl != null)
                {
                    XElement annUrl = new XElement(Namespaces.GetNS("common") + "AnnotationURL");
                    annUrl.Value = annotationType.AnnotationUrl;
                    annotation.Add(annUrl);
                }

                if (annotationType.AnnotationText.Count > 0)
                {
                    foreach (SDMX_Common.TextType textType in annotationType.AnnotationText)
                    {
                        if (textType.Text != null)
                        {
                            XElement annText = new XElement(Namespaces.GetNS("common") + "AnnotationText");
                            annText.Value = textType.Text;

                            if (textType.Lang != null)
                            {
                                annText.Add(new XAttribute(XNamespace.Xml + "lang", textType.Lang));
                            }

                            annotation.Add(annText);
                        }
                    }
                }


                enAnnotations.Add(annotation);
            }

            return enAnnotations;
        }

        internal static XElement GetTextFormatType(SDMX_Structure.TextFormatType textFormatType)
        {
            
            XElement enTextFormat = new XElement(Namespaces.GetNS("structure") + "TextFormat");

            if (textFormatType.Texttype != null)
            {
                enTextFormat.Add(new XAttribute("textType", textFormatType.Texttype.Value.ToString()));
            }

            if (textFormatType.IsSequence != null)
            {
                enTextFormat.Add(new XAttribute("isSequence", textFormatType.IsSequence.ToString().ToLower()));
            }

            if (textFormatType.Minlength != null)
            {
                enTextFormat.Add(new XAttribute("minLength", textFormatType.Minlength));
            }

            if (textFormatType.Maxlength != null)
            {
                enTextFormat.Add(new XAttribute("maxLength", textFormatType.Maxlength));
            }

            if (textFormatType.Startvalue != null)
            {
                enTextFormat.Add(new XAttribute("startValue", textFormatType.Startvalue));
            }

            if (textFormatType.Endvalue != null)
            {
                enTextFormat.Add(new XAttribute("endValue", textFormatType.Endvalue));
            }

            if (textFormatType.Interval != null)
            {
                enTextFormat.Add(new XAttribute("interval", textFormatType.Interval));
            }

            if (textFormatType.Timeinterval != null)
            {
                enTextFormat.Add(new XAttribute("timeInterval", textFormatType.Timeinterval));
            }

            if (textFormatType.Decimals != null)
            {
                enTextFormat.Add(new XAttribute("decimals", textFormatType.Decimals));
            }

            if (textFormatType.Pattern != null)
            {
                enTextFormat.Add(new XAttribute("pattern", textFormatType.Pattern));
            }

            return enTextFormat;
        }
    }
}
